<template>
    <div class="user-div">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <div>
                    <el-input placeholder="按医院名称搜索" suffix-icon="el-icon-search" v-model="form.name" @keyup.native.enter="init(currentPag)">
                    </el-input>
                </div>
                <div>
                    <!-- <el-button type="danger" @click="">删除</el-button> -->
                </div>
            </div>
            <div class="text item">
                <el-table :data="hospitalList" border style="width: 100%;" ref="multipleTable"
                    @selection-change="handleSelectionChange" v-loading="loading">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="ID" width="200">
                    </el-table-column>
                    <el-table-column prop="name" label="医院名称" width="200">
                    </el-table-column>
                    <el-table-column prop="address" label="医院地址" width="300">
                    </el-table-column>
                    <el-table-column prop="introduce" label="医院简介" width="280">
                    </el-table-column>
                    <el-table-column prop="status" label="状态" width="80">
                        <template slot-scope="scope">
                            <el-tag type="success" v-show="scope.row.status == 1">正常</el-tag>
                            <el-tag type="danger" v-show="scope.row.status == 0">禁用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="80">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" v-show="scope.row.status == 0"
                                @click="">恢复</el-button>
                            <el-button type="text" size="small" v-show="scope.row.status == 1"
                                @click="">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination">
                <el-pagination background layout="prev, pager, next" :total="totalPage" :page-size="pageSize"
                    @current-change="currentChange">
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: 'HospitalList',
    data() {
        return {
            // 是否加载
            loading: false,
            // 搜索内容
            searchContent: "",
            // 多选数据
            multipleSelection: [],
            // 当前页数
            currentPag: 1,
            // 每页大小
            pageSize: 10,
            // 总页数
            totalPage: 0,
            // 表单数据
            form: {
                name: null
            }
        }
    },
    mounted() {
        this.init(this.currentPag)
    },
    methods: {
        // 获取表格数据
        async init(page) {
            this.loading = true
            let res = await this.$store.dispatch('getHospitalPag', {
                currentPag: page,
                pageSize: this.pageSize,
                name: this.form.name
            })
            if (!res.success) this.$myfun.resError(res)
            this.loading = false
        },
        // 多选
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        // 更换页数
        currentChange(pag) {
            this.init(pag)
        },
        // 重置
        refresh() {
            let newObj = {}
            this.form = newObj
        },
        // 删除医生
        deleteDoctor(id) {
            this.$confirm('此操作将删除医生, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let res = await this.$store.dispatch("deleteDoctor", id)
                if (!res.success) this.$myfun.resError(res)
                else {
                    this.$message.success(res.message)
                    this.init(this.currentPag)
                }
            }).catch((res) => {
                this.$myfun.resError(res)
            });
        },
        // 恢复
        async returnDoctor(id) {
            let res = await this.$store.dispatch("returnDoctor", id)
            if (!res.success) this.$myfun.resError(res)
            else {
                this.$message.success(res.message)
                this.init(this.currentPag)
            }
        }
    },
    computed: {
        hospitalList() {
            return this.$store.state.hospital.hospitalList
        }
    }
}
</script>

<style scoped>
.clearfix {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.pagination {
    padding: 20px 0 10px 0;
    text-align: center;
}
</style>